import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    redirect: '/',
    component: () => import("views/home/Home.vue"),
    children:[
      {
        path: '/',
        name: 'Index',
        meta:{title:'首页'},
        component: () => import("views/home/index.vue")
      },
      {
        path: '/myapply',
        name: 'myapply',
        meta:{title:'我的申请'},
        component: () => import("views/myapply/applyIndex.vue")
      },
      {
        path: '/myinfo',
        name: 'myinfo',
        meta:{title:'个人中心'},
        component: () => import("views/myinfo/infoIndex.vue")
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta:{title:'登录'},
    component: () => import("views/login/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    meta:{title:'用户注册'},
    component: () => import("views/login/Register.vue"),
  },
  {
    path: "/wechatregister",
    name: "wechatregister",
    meta:{title:'扫码推广二维码'},
    component: () => import("views/login/wechatRegister.vue"),
  },
  {
    path: '/assessment',
    name: 'Assessment',
    meta:{title:'评估申请'},
    component: () => import("views/Assessment.vue")
  },
  {
    path: "/mostbanner",
    name: "mostbanner",
    meta:{title:'banner详情'},
    component: () => import("views/home/Mostbanner.vue"),
  },

  {
    path: '/mymessage',
    name: 'mymessage',
    meta:{title:'个人信息'},
    component: () => import("views/myinfo/mymessage.vue")
  },
  {
    path: '/companymessage',
    name: 'companymessage',
    meta:{title:'企业信息'},
    component: () => import("views/myinfo/companymessage.vue")
  },
  {
    path: '/editname',
    name: 'editname',
    meta:{title:'名字修改'},
    component: () => import("views/myinfo/editName.vue")
  },
  {
    path: '/companyname',
    name: 'companyname',
    meta:{title:'修改'},
    component: () => import("views/myinfo/editCompany.vue")
  },
  {
    path: '/fk',
    name: 'fk',
    meta:{title:'意见反馈'},
    component: () => import("views/myinfo/feedback.vue")
  },
  {
    path: '/setpassword',
    name: 'setpassword',
    meta:{title:'设置密码'},
    component: () => import("views/myinfo/setpassword.vue")
  },
  {
    path: '/setPwdSpeed_1',
    name: 'setPwdSpeed_1',
    meta:{title:'设置密码'},
    component: () => import("views/myinfo/setpassword1.vue")
  },
  {
    path: '/setPwdSpeed_2',
    name: 'setPwdSpeed_2',
    meta:{title:'设置密码'},
    component: () => import("views/myinfo/setpassword2.vue")
  },
  {
    path: '/filetext',
    name: 'filetext',
    meta:{title:'协议'},
    component: () => import("views/myinfo/filetext.vue")
  },
  {
    path: '/accountSecurity',
    name: 'accountSecurity',
    meta:{title:'安全'},
    component: () => import("views/myinfo/AccountSecurity.vue")
  },
  {
    path: '/copyrightInfo',
    name: 'copyrightInfo',
    meta:{title:'版权信息'},
    component: () => import("views/myinfo/copyrightInfo.vue")
  },
  {
    path: '/customer',
    name: 'customer',
    meta:{title:'客服'},
    component: () => import("views/otherinfo/customer.vue")
  },
  {
    path: '/pushmessage',
    name: 'pushmessage',
    meta:{title:'消息'},
    component: () => import("views/otherinfo/pushmessage.vue")
  },
  {
    path: '/bgDetails/:id',
    name: 'bgDetails',
    
    component: () => import("views/BgDetails.vue")
  },
  {
    path: '/report/:uuid',
    name: 'report',
    component: () => import("views/Report.vue")
  },
  {
    path: '/financing/:code',
    name: 'financing',
    meta:{title:'融资服务详情'},
    component: () => import("views/myapply/financing.vue")
  },
  {
    path: '/applysuccess/:code',
    name: 'applysuccess',
    meta:{title:'融资申请-提交成功'},
    component: () => import("views/myapply/applySuccess.vue")
  },
  {
    path: "*",
    redirect: "/"
  }
];

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});


router.beforeEach((to, from, next) => {
  if (to.meta.title) {//如果设置标题，拦截后设置标题
    document.title = to.meta.title
  }

  const token = localStorage.token;
  const isLogin = localStorage.isLogin;
  const openId = localStorage.openId
  switch (to.path) {
    case "/login":
    case "/register":
    case "/setPwdSpeed_1":
    case "/setPwdSpeed_2":
    case "/wechatregister":
      next();
      break;
    default:
      token && isLogin && openId ? next() : router.replace("/login");
  }
  next()
});

export default router;
